/**
 * controller
 * @return 
 */
var fs = require("fs");
module.exports = Controller("Weixin/BaseController", function(){
  "use strict";
  return {
      indexAction: function () {
          //render View/Home/article_edit.html file
          this.assign({
              active:"weixin"
          });
          this.display();
      },
      coverAction: function(){
          this.assign({
              active:"weixin"
          });
          this.display();
      },
      contentAction: function(){
          this.assign({
              active:"weixin"
          });
          this.display();
      },
      loadtempAction:function(){
          var type = this.post("type");
          this.display('temp_'+type)
      },
      xiaoAction:function(){
          var self=this;
          var name=this.get();
          this.session("user",name);
          console.log(222222)
          self.end("fdfssfs");

      },
      imguploadAction:function(){
          //TODO JINWEIRONG
          var self=this;
          var file = self.file("file");
          var oldPath = file.path;
          var oriName = file.originalFilename;//图片原名
          console.log(oriName);
          var conf = F("ueditorconfig");//读取配置文件
          var config = {
              pathFormat: conf['imagePathFormat'],
              maxSize: conf['imageMaxSize'],
              allowFiles: conf['imageAllowFiles']
          }
          var fullname = getFullName(config, oriName);//获得新图片地址
          var filename = getFileName(fullname);//新图片名称
          filename=filename.substr(0,filename.lastIndexOf("."));
          creatdir(fullname).then(function(res){
              if(res){
              savefile(oldPath,fullname).then(function(res){
                  if(res){

                      self.json({"jsonrpc" : "2.0", "result" : "","file_path":res, "id" : filename});
                  }else{

                  }

              })
              }
          })

          //fs.readFile(file.path, function(err, data){
          //    if(err == null) {
          //        console.log("1");
          //        var tmpArr = file.path.split(".");
          //        var fileSuffix = tmpArr[tmpArr.length - 1];
          //        var fileName = new Date().getTime();
          //        var fileDir = times(fileName);
          //
          //
          //
          //        //fs.exists(fileDir, function (err) {
          //        //    console.log("2", err);
          //        //    if (err == false) {
          //        //        console.log("3");
          //        //        //mkdir(fileDir);
          //        //        fs.mkdir(fileDir, function(err){
          //        //            fs.writeFile(fileDir + "/" + fileName+(Math.round(Math.random()*1000000))+ "." + fileSuffix, data);
          //        //        });
          //        //    }
          //        //    else {
          //        //        fs.writeFile(fileDir + "/" + fileName+(Math.round(Math.random()*1000000))+ "." + fileSuffix, data);
          //        //    }
          //        //    //self.json({"id":"1", "file_path":+fileDir+"/"+fileName+"."+fileSuffix});
          //        //});
          //    }
          //});

      },
      defAction:function(){
          function a(){
              var deferred = getDefer();
              function aa(){
                  console.log("aa");
                  deferred.resolve('我已经读完文件拿到内容文件名');

              };
              setTimeout(aa,10000)
              return deferred.promise;
          }

          function b(can){
              var deferred = getDefer();
                  console.log("bb");
                  deferred.resolve(can+':我等aa给我文件名，我再去读');
              return deferred.promise;
          }
          a().then(function(res){
              b(res).then(function(re){
                  console.log(re);
              });
          });
          this.end();
      }
  }
});